package de.uni_mannheim.informatik.ai.dame_ai.dame.application;



public class ReplayThread implements Runnable{
	private DameFacade game;
	private Thread thread;
	private long updateRate = 4000;
	public long getUpdateRate() {
		return updateRate;
	}
	public void setUpdateRate(long updateRate) {
		this.updateRate = updateRate;
	}
	public ReplayThread(DameFacade game){
		this.game = game;
	}
	@Override
	public void run() {
		while(!isStop()){
			if(isRunning()){
				
			
				try {
					Thread.sleep(updateRate);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(running)game.nextMove();
			}
		}
		
	}
	boolean running = true;
	synchronized public void setRunning(boolean running){
		this.running = running;
		if(running)notifyAll();
	}
	public boolean getRunning(){
		return running;
	}
	synchronized private boolean isRunning(){
		if(running){
			return true;
		} else {
			try {
				wait();
				
			} catch (InterruptedException e) {
			}
			return true;
		}
	}
	boolean stop = false;
	synchronized public void setStop(boolean stop){
		this.stop = stop;
		this.running = false;
		if(stop)notifyAll();
	}
	synchronized public boolean isStop(){
		return stop;
	}
	synchronized public void start(){
		if ( thread == null ) { 
            thread = new Thread(this); 
            thread.start(); 
        } 

	}
}
